//
//  SERegistController.m
//  kuaiyi
//
//  Created by Alphts on 16/7/5.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SERegistController.h"
#import <SMS_SDK/SMSSDK.h>
#import "SMS_HYZBadgeView.h"
#import <SMS_SDK/Extend/SMSSDK+DeprecatedMethods.h>
#import <SMS_SDK/Extend/SMSSDK+ExtexdMethods.h>
#import "SMSSDKUI.h"

@interface SERegistController ()

//手机号field
@property (nonatomic, strong)UITextField * tf_phoneNum;

//验证码field
@property (nonatomic, strong)UITextField * tf_replyNum;

//获取验证码btn
@property (nonatomic, strong)UIButton * btn_getNum;

//协议label
@property (nonatomic, strong)UILabel * lb_protocal;

//同意协议btn
@property (nonatomic, strong)UIButton * btn_allow;

//注册btn
@property (nonatomic, strong)UIButton * btn_regist;

//计时器
@property (nonatomic, strong)NSTimer * timer;
//秒数
@property (nonatomic, assign)NSInteger second;

@end

@implementation SERegistController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setupUI];
}

- (void)setupUI {
    self.title = @"注册";
    
    //添加控件
    [self.view addSubview:self.tf_phoneNum];
    [self.view addSubview:self.tf_replyNum];
    [self.view addSubview:self.btn_getNum];
    [self.view addSubview:self.btn_allow];
    [self.view addSubview:self.lb_protocal];
    [self.view addSubview:self.btn_regist];
    
    //设置约束
    [self.tf_phoneNum makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64+40);
        make.height.mas_equalTo(35);
        make.left.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.tf_replyNum makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.btn_getNum.mas_top);
        make.height.mas_equalTo(35);
        make.left.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.btn_getNum.mas_left).offset(-20);
    }];
    
    [self.btn_getNum makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tf_phoneNum.mas_bottom).offset(30);
        make.right.mas_equalTo(self.tf_phoneNum.mas_right);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(100);
    }];
    
    [self.btn_allow makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.tf_replyNum.mas_bottom).offset(45);
        make.left.mas_equalTo(self.tf_replyNum.mas_left).offset(40);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.lb_protocal makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.btn_allow);
        make.left.mas_equalTo(self.btn_allow.mas_right).offset(10);
        make.height.equalTo(self.btn_allow);
        make.right.mas_equalTo(self.view).offset(-20);

    }];
    
    [self.btn_regist makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.btn_allow.mas_bottom).offset(20);
        make.height.mas_equalTo(45);
        make.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
    }];
    
}

#pragma mark
#pragma mark - 计时器方法
- (void)timerGoing {
    NSString * stateStr = @"";
    
    if (self.second <= 0) {
        stateStr = @"获取验证码";
        self.btn_getNum.userInteractionEnabled = YES;
        [self.timer invalidate];
        self.timer = nil;
    
    }else{
        self.btn_getNum.userInteractionEnabled = NO;
        stateStr = [NSString stringWithFormat:@"%lds",self.second];
    }
    self.btn_getNum.titleLabel.text = @"60s";
    [self.btn_getNum setTitle:stateStr forState:UIControlStateNormal];
    self.second -= 1;
}

#pragma mark
#pragma mark - 点击事件
//同意协议按钮
- (void)agreeProtocol:(UIButton *)sender {
    sender.selected = !sender.selected;
}
//获取验证码按钮
- (void)clickGetReplyNumber:(UIButton *)sender {
    self.second = 10;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerGoing) userInfo:nil repeats:YES];
    
    [self.timer fire];
    
    //18856062670
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.tf_phoneNum.text
                                   zone:@"86"
                       customIdentifier:nil
                                 result:^(NSError *error){
     if (!error) {
         NSLog(@"获取验证码成功");
     } else {
         NSLog(@"错误信息：%@",error);
     }}];
        
}
//注册按钮
- (void)clickRegistBtn:(UIButton *)sender {
    NSLog(@"正在注册");
    
//    [SMSSDK commitVerificationCode:self.tf_replyNum.text phoneNumber:@"18856062670"
//                              zone:@"86" result:^(NSError *error) {
//        
//        if (!error) {
//            
//            NSLog(@"验证成功");
//            
//        }
//        else
//        {
//            NSLog(@"错误信息：%@",error);
//            
//        }
//    }];
}

#pragma mark
#pragma mark - 懒加载
- (UITextField *)tf_phoneNum {
    if (nil == _tf_phoneNum) {
        _tf_phoneNum = [[UITextField alloc]init];
        _tf_phoneNum.placeholder = @"请输入您的手机号码";
        _tf_phoneNum.clearButtonMode = UITextBorderStyleRoundedRect;
        _tf_phoneNum.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _tf_phoneNum;
}

- (UITextField *)tf_replyNum {
    if (nil == _tf_replyNum) {
        _tf_replyNum = [[UITextField alloc]init];
        _tf_replyNum.placeholder = @"请输入验证码";
        _tf_replyNum.clearButtonMode = UITextBorderStyleRoundedRect;
        _tf_replyNum.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _tf_replyNum;
}

- (UIButton *)btn_getNum {
    if (nil == _btn_getNum) {
        _btn_getNum = [[UIButton alloc]init];
        [_btn_getNum setTitle:@"获取验证码" forState:UIControlStateNormal];
        _btn_getNum.layer.cornerRadius = 3;
        _btn_getNum.layer.masksToBounds = YES;
        _btn_getNum.titleLabel.font = FONT(16);
        [_btn_getNum setBackgroundColor:[UIColor grayColor]];
        
        [_btn_getNum addTarget:self action:@selector(clickGetReplyNumber:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_getNum;
}

- (UIButton *)btn_allow {
    if (nil == _btn_allow) {
        _btn_allow = [[UIButton alloc]init];
        [_btn_allow setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
        [_btn_allow setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
        
        [_btn_allow addTarget:self action:@selector(agreeProtocol:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_allow;
}

- (UILabel *)lb_protocal {
    if (nil == _lb_protocal) {
        _lb_protocal = [[UILabel alloc]init];
        _lb_protocal.font = FONT(13);
        
        NSString * baseStr = @"本人同意并阅读 用户协议";
        
        NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:baseStr];
        
        NSRange range = [baseStr rangeOfString:@"用户协议"];
        
        [string addAttributes:@{NSFontAttributeName:FONT(14),NSForegroundColorAttributeName:BLUE_COLOR} range:range];
        
        _lb_protocal.attributedText = string;
    }
    return _lb_protocal;
}

- (UIButton *)btn_regist {
    if (nil == _btn_regist) {
        _btn_regist = [[UIButton alloc]init];
        [_btn_regist setTitle:@"注册" forState:UIControlStateNormal];
        [_btn_regist setBackgroundColor:kThemeColor];
        _btn_regist.titleLabel.font = FONT(20);
        
        [_btn_regist addTarget:self action:@selector(clickRegistBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_regist;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


@end














